package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Unit;
import com.woniuxy.caradmin.entity.vo.UnitVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.UnitService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/unit")
public class UnitController {
    @Autowired
    private UnitService unitService;
    @ApiOperation(value = "获取往来单位信息")
    @GetMapping(value = "/selectUnitAll")
    public R selectUnitAll(){
        List<Unit> units = unitService.selectUnitAll();
        if(units!=null&&units.size()>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS).data("units",units);
        }else{
            throw new CarException(ResultCode.UNITLISTNULL,Constaint.UNITLISTNULL);
        }
    }
    @ApiOperation(value = "获取往来单位信息")
    @GetMapping(value = "/selectUnitById")
    public R selectUnitById(Integer unitid){
        Unit unit = unitService.selectUnitById(unitid);
        if (unit != null) {
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS).data("unit",unit);
        }else{
            throw new CarException(ResultCode.UNITISNULL,Constaint.UNITISNULL);
        }
    }
    @ApiOperation(value = "增加往来单位信息")
    @PostMapping(value = "/addUnit")
    public R addUnit(HttpServletRequest request){
        String name = request.getParameter("name");
        String unittypeid = request.getParameter("unittype");
        String telephone = request.getParameter("telephone");
        String contact = request.getParameter("contact");
        String information = request.getParameter("information");
        String available = request.getParameter("available");
        Unit unitAdd = new Unit();
        unitAdd.setName(name);
        unitAdd.setUnittypeid(Integer.parseInt(unittypeid));
        unitAdd.setTelephone(telephone);
        unitAdd.setContact(contact);
        unitAdd.setInformation(information);
        unitAdd.setAvailable(Integer.parseInt(available));
        boolean flag = unitService.addUnit(unitAdd);
        if(flag){
            return R.ok().code(ResultCode.ADDUNITSUC).message(Constaint.ADDUNITSUC);
        }else{
            throw new CarException(ResultCode.ADDUNITFAIL,Constaint.ADDUNITFAIL);
        }
    }
    @ApiOperation(value = "软删除往来单位信息")
    @GetMapping(value = "/removeUnitById")
    public R removeUnitById(Integer id){
        boolean flag = unitService.removeUnitById(id);
        if(flag){
            return R.ok().code(ResultCode.REMOVEUNITSUC).message(Constaint.REMOVEUNITSUC);
        }else{
            throw new CarException(ResultCode.REMOVEUNITFAIL,Constaint.REMOVEUNITFAIL);
        }
    }
    @ApiOperation(value = "更新往来单位信息")
    @GetMapping(value = "/updateUnit")
    public R updateUnit(HttpServletRequest request){
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String unittypeid = request.getParameter("unittype");
        String telephone = request.getParameter("telephone");
        String contact = request.getParameter("contact");
        String information = request.getParameter("information");
        String available = request.getParameter("available");
        Unit unitUpd = new Unit();
        unitUpd.setId(Integer.parseInt(id));
        unitUpd.setName(name);
        unitUpd.setUnittypeid(Integer.parseInt(unittypeid));
        unitUpd.setTelephone(telephone);
        unitUpd.setContact(contact);
        unitUpd.setInformation(information);
        unitUpd.setAvailable(Integer.parseInt(available));
        boolean flag = unitService.updateUnit(unitUpd);
        if(flag){
            return R.ok().code(ResultCode.UPDUNITSUC).message(Constaint.UPDUNITSUC);
        }else{
            throw new CarException(ResultCode.UPDUNITFAIL,Constaint.UPDUNITFAIL);
        }
    }
    @ApiOperation(value = "往来单位分页")
    @GetMapping(value = "/getUnitList")
    public DataGridView getUnitList(UnitVo unitVo){
        Page<Object> page = PageHelper.startPage(unitVo.getPage(), unitVo.getLimit());
        List<Unit> unitList = unitService.selectUnitByPage(unitVo);
        if(unitList!=null&&unitList.size()>0){
            return new DataGridView(page.getTotal(),unitList);
        }throw new CarException(ResultCode.ERROR,Constaint.ERROR);
    }
    @ApiOperation(value = "批量软删除单位")
    @GetMapping(value = "/removeUnitBatch")
    public R removeUnitBatch(Integer[] ids){
        boolean flag = unitService.removeUnitBatch(ids);
        if(flag){
            return R.ok().code(ResultCode.REMOVEUNITSUC).message(Constaint.REMOVEUNITSUC);
        }else{
            throw new CarException(ResultCode.REMOVEUNITFAIL,Constaint.REMOVEUNITFAIL);
        }
    }
}

